package k3;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.security.InvalidParameterException;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

public class Shape extends JPanel implements MouseListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Table table;
	private String shape;
	private Timer timer;
	private int interval, locCounter = 1, remNr = 4, gameEndNr = 5, negPoints = -1;
	private CheckLoc cl = new CheckLoc();
	private Shape sh = this;
	public Shape(String shape, Table table){
		this.addMouseListener(this);
		this.table = table;
		this.shape = shape;
		if(shape.equalsIgnoreCase("circle")){
			this.setSize(50, 50);
			interval = 1000;
		}else if(shape.equalsIgnoreCase("square")){
			this.setSize(25, 25);
			interval = 2000;
		}
		this.setLocation(getCord("x"), getCord("y"));
		timer = new Timer(interval, cl);
		timer.start();
		this.setVisible(true);
	}
	public void paint(Graphics g){
		super.paintComponent(g);
		if(shape.equalsIgnoreCase("circle")){
			g.setColor(Color.BLUE);
			g.fillOval(0, 0, getWidth(), getHeight());
		}else if(shape.equalsIgnoreCase("square")){
			g.setColor(Color.RED);
			g.fillRect(0, 0, getWidth(), getHeight());
			
		}
	}
	private class CheckLoc implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			changeLocation();
			if(locCounter == remNr){
				timer.stop();
				table.remove(sh);
				table.getList().remove(sh);
				table.setCurrentPoints(table.getCurrentPoints()-negPoints);
				table.setUnClicked(table.getUnClicked()+1);
				addShape();
			}
			if(table.getUnClicked() == gameEndNr){
				timer.stop();
				table.setOver(true);
			}
		}	
	}
	private void changeLocation(){
		int generatedX = 0, generatedY = 0;
		boolean occupied = false;
		Rectangle ract = new Rectangle(generatedX = getCord("x"), generatedY = getCord("y"), getWidth(), getHeight());
		for(Shape s : table.getList()){
			Rectangle used = new Rectangle(s.getX(), s.getY(), s.getWidth(), s.getHeight());
			if(ract.intersects(used)){
				occupied = true;
				changeLocation();
			}
		}
		if(!occupied){
			locCounter++;
			timer.setDelay(interval-400);
			this.setLocation(generatedX, generatedY);
		}
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		timer.stop();
		int userPoints = table.getCurrentPoints();
		table.remove(this);
		table.repaint();
		if(locCounter == 1){
			table.setCurrentPoints(userPoints + 3);
		}else if(locCounter == 2){
			table.setCurrentPoints(userPoints + 2);
		}else if(locCounter == 3){
			table.setCurrentPoints(userPoints + 1);
		}
		addShape();
	}
	public void addShape(){
		int nextShape;
		Random rnd = new Random();
		nextShape = rnd.nextInt(2);
		if(nextShape==0){
			table.add("circle");
		}else if(nextShape==1){
			table.add("square");
		}
	}
	public int getCord(String cords){
		Random cord = new Random();
		if(cords.equalsIgnoreCase("x")){
			return cord.nextInt(table.getHeight()-100);
		}else if(cords.equalsIgnoreCase("y")){
			return cord.nextInt(table.getWidth()-100);
		}else{
			throw new InvalidParameterException();
		}
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}
}
